<template>
  <div class="login-container">
    <el-form autoComplete="off" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px"
      class="card-box login-form">
      <h3 class="ww-logo"></h3>
      <el-form-item  class="ww-border" prop="username" >
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
        <el-input auto-complete="off" name="username" type="text" v-model="loginForm.username"  placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item  class="ww-border"  prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="password" trigger-on-focus="false" autoComplete="off" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password"
          placeholder="请输入密码" @change="showPwd"></el-input>
          <!--<span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>-->
      </el-form-item>
      <el-form-item >
          <el-button class="pull-left" style="display: inline-block;width: 100%;" type="primary" :loading="loading" @click.native.prevent="handleLogin">
            登录
          </el-button>
      </el-form-item>
      <el-form-item style="text-align: center;">
        <span class="register" style="text-decoration:underline;font-size: 18px;color: #4895FF;" @click="reigster">注册</span>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { validatMobile, isvalidUsername } from '@/utils/validate'
import { Message } from 'element-ui'

export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      // if (isvalidUsername(value)) {
      //   callback()
      // } else
      if (!validatMobile(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      pwdType: 'text'
    }
  },
  methods: {
    showPwd() {
      // if (this.pwdType === 'password') {
      //   this.pwdType = ''
      // } else {
      //   this.pwdType = 'password'
      // }
      this.pwdType = 'password'
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {

            this.loading = false
            this.$router.push({ path: '/' })
          }).catch(() => {
            Message.error('登录失败,请检查账号密码是否输入正确。')
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    reigster() {
      this.$router.push({ path: '/register' })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/mixin.scss";
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;
  $Color_clear: rgba(0,0,0,0);
  .register:hover{
    color: white;
  }
  .el-form-item__content{
    background: rgba(7,18,46,0.0);
  }
  .el-input{
    background: none;
  }
  .login-container {
    @include relative;
    min-height: 750px;
    background: url("../../assets/images/login-bg.png") no-repeat center center ;
    background-size: 1280px 730px;
    margin-top: 100px;
    // background-color: $bg;
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px rgba(0,0,0,0) inset !important;
      background-color: rgba(250, 255, 189,0) !important;
      -webkit-text-fill-color: rgba(0,0,0,0) !important;
    }
    .ww-border{
      background: rgba(7,18,46,0.50);
      border: 2px solid #0099FF;
      border-radius: 2px;
    }
    input {
      background: rgba(0,0,0,0);
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
    }
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: #0099FF;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      font-size: 41.58px;
      color: #FFFFFF;
    }
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 400px;
      padding: 13px 35px 15px 35px;
      margin: 140px auto;
      /*margin-left: 430px !important;*/
    }
    .el-form-item {
      background: $Color_clear;
      border-radius: 5px;
      color: #454545;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select:none;
    }
    .thirdparty-button{
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
  }
</style>
